//
//  HMComposeToolBar.swift
//  Weibo22
//
//  Created by Apple on 16/12/9.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

//   toolbar按钮枚举类型
enum HMComposeToolBarButtonType: Int {
    //  图片
    case picture = 0
    //  @
    case mention = 1
    //  #
    case trend = 2
    //  表情
    case emoticon = 3
    //  加号
    case add = 4
}


//  发微博界面的toolbar
//  它只是一个容器,不具备渲染效果,依赖子控件的显示
class HMComposeToolBar: UIStackView {
    
    //  执行点击toolbar的闭包
    var callBack: ((HMComposeToolBarButtonType)->())?
    //  表情按钮
    var emoticonButton: UIButton?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        
        //  设置布局方式
        axis = .horizontal
        //  子控件的填充方式
        distribution = .fillEqually
        
        addChildButton(imageName: "compose_camerabutton_background", type: .picture)
        addChildButton(imageName: "compose_mentionbutton_background", type: .mention)
        addChildButton(imageName: "compose_trendbutton_background", type: .trend)
        emoticonButton = addChildButton(imageName: "compose_emoticonbutton_background", type: .emoticon)
        addChildButton(imageName: "compose_add_background", type: .add)
    }
    
    //  添加子按钮
    //  表示可以废弃的结果 swift 3.0 如果函数有返回值,但是没有处理就警告
    @discardableResult
    private func addChildButton(imageName: String, type: HMComposeToolBarButtonType) -> UIButton {
    
        let button = UIButton()
        //  rawValue ,表示枚举的原始值
        button.tag = type.rawValue
        button.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
        button.setImage(UIImage(named: imageName), for: .normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), for: .normal)
        //  取消高亮效果
        button.adjustsImageWhenHighlighted = false
        //  添加按钮
        addArrangedSubview(button)
        
        return button
        
    }
    
    //  MARK: -- 点击toolbar按钮
    @objc private func btnAction(btn: UIButton) {
    
        //  通过枚举的原始值创建枚举类型的成员
        let type = HMComposeToolBarButtonType(rawValue: btn.tag)!
        
        //  执行闭包
        callBack?(type)
        
    
    }
    //  根据键盘类型设置不同状态的icon
    func showIcon(isEmoticon: Bool) {
        
        if isEmoticon {
            //  表示表情键盘类型 -> 设置系统icon
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), for: .normal)
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), for: .highlighted)
            
        } else {
            //  表示系统键盘类型 -> 设置表情icon
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), for: .normal)
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), for: .highlighted)
        }
    
    }
    
   

}
